package com.ljd.controller.front;

import com.ljd.client.OrderClient;
import com.ljd.entity.frontVo.CourseFrontVo;
import com.ljd.entity.frontVo.CourseWebVo;
import com.ljd.entity.vo.ChapterVo;
import com.ljd.service.EduChapterService;
import com.ljd.service.EduCourseService;
import com.ljd.util.JwtUtils;
import com.ljd.util.R;
import com.ljd.util.vo.CourseWebByOrderVo;
import com.why.handler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import net.sf.jsqlparser.statement.select.OrderByVisitor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
//@CrossOrigin
@RequestMapping("eduservice/CourseFront")
@Api(description = "前台课程")
public class CourseFrontController {
    @Autowired
    EduCourseService courseService;
    @Autowired
    EduChapterService chapterService;

    @Autowired
    OrderClient orderClient;
    @PostMapping("getCourseSByCondition/{current}/{pageSize}")
    public R getCourseByCondition(@PathVariable Long current, @PathVariable Long pageSize,
                                  @RequestBody(required = false) CourseFrontVo courseFrontVo){
        Map<String,Object> map = courseService.getCourseByCondition(current,pageSize,courseFrontVo);
        return R.ok().data("map",map);
    }
    @GetMapping("getCourseDetail/{courseId}")
    public R getCourseDetail(
            @ApiParam(name = "courseId",value = "课程ID",required = true)
            @PathVariable String courseId, HttpServletRequest request){
        // 查询课程信息和讲师信息
        CourseWebVo courseWebVo = courseService.getCourseWebVoById(courseId);
        // 查询改课程付费课程是否已购买
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if(StringUtils.isEmpty(memberId)){
           throw new GuliException(20001,"用户还未登陆");
        }
        Boolean isBuy = orderClient.getOrder(courseId, memberId);
        // 查询章节信息
        List<ChapterVo> chapterVos = chapterService.getChapterAndVideo(courseId);
        return R.ok().data("courseWebVo",courseWebVo).data("chapterVos",chapterVos).data("isBuy",isBuy);
    }
    @GetMapping("getCourseInfo/{courseId}")
    public CourseWebByOrderVo getCourseInfo(@PathVariable String courseId){
        CourseWebByOrderVo courseWebByOrderVo = new CourseWebByOrderVo();
        CourseWebVo courseWebVo = courseService.getCourseWebVoById(courseId);
        BeanUtils.copyProperties(courseWebVo,courseWebByOrderVo);
        return courseWebByOrderVo;
    }
}
